package org.gnyxy.controller.background;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.gnyxy.constant.StatusConstant;
import org.gnyxy.dto.CarouselPageQueryDTO;
import org.gnyxy.entity.Carousel;
import org.gnyxy.result.PageResult;
import org.gnyxy.result.Result;
import org.gnyxy.service.CarouselService;
import org.gnyxy.vo.CarouselVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

@RestController("AdminCarouselController")
@RequestMapping("/background/carousel")
@Slf4j
@Api(tags = "商家后台-轮播图管理")
public class CarouselController {
    @Autowired
    private CarouselService carouselService;

    /**
     * 添加轮播图
     * @param carousel
     */
    @PostMapping("/add")
    @ApiOperation("新增轮播图")
    @CacheEvict(cacheNames = "CarouselCache",allEntries = true)
    public Result add(@RequestBody Carousel carousel){
        log.info("新增轮播图: {}",carousel);
        carouselService.add(carousel);
        return Result.success();
    }

    /**
     * 分页
     * @param carouselPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页管理展示轮播图")
    public Result<PageResult> page(CarouselPageQueryDTO carouselPageQueryDTO){
        log.info("分页管理展示轮播图: {}",carouselPageQueryDTO);
        PageResult result = carouselService.page(carouselPageQueryDTO);
        return Result.success(result);
    }

    /**
     * 更改轮播图信息
     * @param carousel
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("更改轮播图信息")
    @CacheEvict(cacheNames = "CarouselCache",allEntries = true)
    public Result update(@RequestBody Carousel carousel){
        log.info("更改轮播图信息: {}",carousel);
        carouselService.update(carousel);
        return Result.success();
    }

    /**
     * 根据id查询轮播图信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询轮播图信息")
    public Result<CarouselVO> getById(@PathVariable Long id){
        log.info("查询轮播图id为: {}",id);
        CarouselVO carouselVO = carouselService.getById(id);
        return Result.success(carouselVO);
    }

    /**
     * 删除轮播图
     * @return
     */
    @PostMapping("/delete/{id}")
    @ApiOperation("删除轮播图")
    @CacheEvict(cacheNames = "CarouselCache",allEntries = true)
    public Result deleteById(@PathVariable Long id){
        log.info("删除轮播图id: {}",id);
        carouselService.deleteById(id);
        return Result.success();
    }

    /**
     * 设置状态
      * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("设置状态")
    @CacheEvict(cacheNames = "CarouselCache",allEntries = true)
    public Result setStatus(@PathVariable Integer status,Long id){
        log.info("设置轮播图id: {},状态为: {}",id,status.equals(StatusConstant.ENABLE) ? "启用":"禁用");
        carouselService.setStatus(status,id);
        return Result.success();
    }
}
